/**
 * Created by a1 on 16/9/23.
 */

//2->杂谈列表
var listType
var listData

var nodeListTitle
var nodeListContent
var nodeList

var Size = 10
var timeStamp = 0
var isLoading = false
var arrayData

function initList() {
    nodeListTitle = $("#dm_title_list")
    nodeListContent = $("#dm_content_list")
    nodeList = $("#dm_list")

    bindRefresh(nodeListContent, function() {
        timeStamp = 0
        apiZaTanList()
    })
    $.pullToRefreshTrigger(nodeListContent)
}

function onShowList() {
    nodeListTitle = $("#dm_title_list")
    nodeListContent = $("#dm_content_list")
    nodeList = $("#dm_list")
}

function initWithData() {
    if(listType == 1 || listType == 2) {
        nodeListTitle.html(listData[kTitle])
        bindRefresh(nodeListContent, function() {
            timeStamp = 0
            apiZaTanList()
        })
        $.pullToRefreshTrigger(nodeListContent)
    }
}

function clearList() {
    arrayData.length = 0
    nodeListContent.scrollTop(0)
    nodeListTitle.html("")
    nodeList.html("")
}

function apiZaTanList() {
    if(this.isLoading) {
        return
    }
    this.isLoading = true
    var url = "../test/data"
    $.getJSON(url, function(data){
        this.isLoading = false
        $.pullToRefreshDone(nodeListContent)
        var length = data.length
        //console.log("data = " + length)
        if(length > 0) {
            if(timeStamp == 0) {
                arrayData.length = 0
                nodeList.html("")
                $.refreshScroller()
                if(length >= Size) {
                    addLoadMore(nodeListContent)
                    bindLoadMore($("#dm_page_list"), function() {
                        console.log("load more list")
                        apiZaTanList()
                    })
                }
            }
            timeStamp = 1
        }
        if(length < Size) {
            removeLoadMore(nodeListContent)
        }
        arrayData = arrayData.concat(data)
        for(var i = 0; i < length; ++i) {
            if(listType == 1 || listType == 2) {
                var itemData = data[i]
                addListItem(nodeList, itemData, "3")
            }
        }
        $.refreshScroller()
    })
}

//function addItemZatan(parent, index, data) {
//    //console.log(index + " = " + JSON.stringify(data))
//    var item = $("<div></div>")
//    var content = $("<div class='dm_list_item' container='middle'></div>")
//    var containerLeft = $("<div class='dm_list_item_container_left' container='middle'></div>")
//    var labelTitle = $("<div class='dm_list_item_text_title'>"+data[kTitle]+"</div>")
//    var labelContent = $("<div class='dm_list_item_text_content'  container='middle'>"+data[kContent]+"</div>")
//    var labelDate = $("<div class='dm_list_item_text_date'>"+data[kDate]+"</div>")
//    containerLeft.append(labelTitle)
//    containerLeft.append(labelContent)
//    containerLeft.append(labelDate)
//    content.append(containerLeft)
//
//    var imageUrl = data[kImageUrl]
//    var hasImage = ($.trim(imageUrl) != "")
//    if(hasImage) {
//        var imageView = $("<img class='dm_list_item_image' container='middle' src='"+imageUrl+"'/>")
//        content.append(imageView)
//        //重置文本与item的size
//        containerLeft.addClass("dm_shrink")
//        content.addClass("dm_shrink")
//    }
//    var divider = "<div class='dm_divider'></div>"
//    item.append(content)
//    item.append(divider)
//    parent.append(item)
//
//    item.click(function() {
//        //toDetail(data)
//    })
//}